﻿using System.Collections.Generic;
using System.Linq;

namespace Lasalara.Model
{
    public class User
    {
        private Chapter _currentChapter;

        public Book CurrentBook { get; set; }

        public bool HasSeenTutorial { get; set; }

        public string Email { get; set; }

        public string UserKey { get; set; }

        public List<Book> NotDownloadedUserBooks { get; set; }

        public List<Book> DownloadedUserBooks { get; set; }

        public Book TutorialBook { get; set; }

        public Chapter CurrentChapter
        {
            get { return _currentChapter; }
            set { _currentChapter = value; }
        }

        public bool IsLoggedIn { get; set; }

        public byte[] Password { get; set; }

        public double CurrentFontSize { get; set; }

        public User()
        {
        }

        public User(string email, string userKey, List<Book> allUserBooks, List<Book> downloadedUserBooks,
                    bool loggedIn, byte[] encryptedPass)
        {
            Email = email;
            UserKey = userKey;
            NotDownloadedUserBooks = allUserBooks;
            DownloadedUserBooks = downloadedUserBooks;
            IsLoggedIn = loggedIn;
            Password = encryptedPass;
        }


        /// <summary>
        /// Insert currentchapter to the users downloaded books list.
        /// </summary>
        public void InsertChapterToDownloadedBook()
        {
            var chapter = _currentChapter;
            var downloaded = DownloadedUserBooks.ToList();
            for (var j = 0; j < downloaded.Count(); j++)
            {
                if (downloaded[j].Chapters.Contains(chapter))
                {
                    var chapters = downloaded[j].Chapters.ToList();
                    for (var i = 0; i < chapters.Count(); i++)
                    {
                        if (chapters[i].Equals(chapter))
                        {
                            chapters[i] = chapter;
                            downloaded[j].Chapters = chapters;
                            return;
                        }
                    }
                }
            }
        }
    }
}